package dbTools.XML;

import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import projectsupporter.common.control.Path;

import dbTools.DBToolEngine;
import dbTools.tableInfo.field.Field;
import dbTools.tableInfo.field.fieldInfo.type.types.TypeReference;
import dbTools.tableInfo.field.fieldValidator.FieldValidator;
import dbTools.tableInfo.field.fieldValidator.param.FieldParm;
import dbTools.tableInfo.info.TableInfo;

public class ValidatorXMLService {

	public static void createValidatorXMLFiles(Map<String,TableInfo> tableInfos,Path mainPath,boolean folderById)
	{
		for(Map.Entry<String,TableInfo> entry : tableInfos.entrySet())
		{
			//mainFileName
			TableInfo tableInfo= entry.getValue();
			String key = tableInfo.getKey();
			
			boolean hadChecker=false;
			
			Document doc = org.dom4j.DocumentHelper.createDocument();
			 doc.addDocType("validators",
					 "-//Apache Struts//XWork Validator 1.0.3//EN",
					 "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd");
			 
			 Element root = doc.addElement("validators");
			 
			 for(Map.Entry<String,Field> fieldEntry : tableInfo.getAllFields().entrySet())
			 {
				 Field field=fieldEntry.getValue();
				 
				 if(field.getCheckers().size()>0)
				 {
					 hadChecker=true;
					 
					 Element fieldElement=root.addElement("field");
					 fieldElement.addAttribute("name", getValidateFieldName(tableInfo,field));
					 
					 for(FieldValidator validator : field.getCheckers())
					 {
						 validator.initParms(field);
						 Element validatorElement=fieldElement.addElement("field-validator");
						 validatorElement.addAttribute("type", validator.getType());
						 
						 if(validator.getParms()!=null)
						 {
							 for(FieldParm parm: validator.getParms())
							 {
								 Element parmElement=validatorElement.addElement("param");
								 parmElement.addAttribute("name", parm.getName());
								 parmElement.setText(parm.getValue());
							 }
						 }
						 
						 if(validator.getMsg()!=null)
						 {
							 Element msgElement=validatorElement.addElement("message");
							 if(validator.getMsg().getKey()!=null)
							 {
								 msgElement.addAttribute("key", validator.getMsg().getKey());
							 }
							 if(validator.getMsg().getValue()!=null)
							 {
								 msgElement.setText(validator.getMsg().getValue());
							 }
						 }
					 }
						 
				 }
			 }
			 
			 if(hadChecker == false)
			 {
				 continue;
			 }
			 
			 String path=mainPath.getAbsolutePath();
			 if(folderById)
			 {
				 path+="\\"+tableInfo.getTableId();
			 }
			 CommonXmlCreator.createXML(doc, path,key+"-validation.xml");
		}
	}

	private static String getValidateFieldName(TableInfo tableInfo , Field field) {
		
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		String idStirng2=(field.isKey()&&isMkey)?"id.":"";

		
		 TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);
		 if(ref==null)
		 {
		    return idStirng2+field.getFieldName();
		 }
		 else
		 {
			 TableInfo refTable=DBToolEngine.findTable(ref.getReferenceId());
			 Field refField=refTable.findField(ref.getReferenceField());
			 boolean isrefMkey=refTable.getKeyFields().size()>1;
			 
				String refidStirng2=(refField.isKey()&&isrefMkey)?"id.":"";
			 
			 if(refTable==null)
			 {
				 return idStirng2+field.getFieldName();
			 }

			 return idStirng2+field.getFieldName()+"."+refidStirng2+refField.getFieldName();
		 }
	}
}
